/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import testcases.MainTest;

import com.trentech.billcalc.FinanceManager;

/**
 * StartLeaseEvent creates a new Lease, and generates BillEvents for the rent
 * up to today's date.
 * 
 * @author Trent Hoeppner
 */
public class StartLeaseEvent extends MoneyEvent {

    /**
     * The name of the tenant.
     */
    private String payerName;
    
    
    /**
     * The date that the lease will end (exclusive).
     */
    private Date leaseEnd;
    
    
    /**
     * The numberof months between payments.
     */
    private int paymentFrequency;
    
    
    /**
     * The amount of money for the deposit.
     */
    private int deposit;
    
    
    /**
     * True if the tenant can sublet, false otherwise.
     */
    private boolean canSublet;
    
    
    /**
     * A description of the apartment or room.
     */
    private String roomDescription;
    
    
    /**
     * The amount of money to charge if the lease is terminated early.
     */
    private int leaseBreakPenalty;

    
    /**
     * Constructor for StartLeaseEvent.
     * 
     * @param   leaseStart          the date that the lease starts.  Cannot be 
     *                              null.
     * @param   controllerName      the name of the landlord or subletter.  
     *                              Cannot be null or empty.
     * @param   amountPerMonth      the amount of rent per month.  Must be 
     *                              >= 0.
     * @param   payerName           the name of the tenant.  Cannot be null or 
     *                              empty.
     * @param   leaseEnd            the date that the lease ends (exclusive).  
     *                              Cannot be null.
     * @param   paymentFrequency    the number of months between payments.  
     *                              Must be > 0.
     * @param   deposit             the deposit amount.  Must be >= 0.
     * @param   canSublet           true indicates that the tenant can sublet, 
     *                              false indicates that subletting is not 
     *                              allowed.
     * @param   roomDescription     a description of the apartment or room (to 
     *                              differentiate it from other rooms).  
     *                              Cannot be null.
     * @param   leaseBreakPenalty   the amount of money that is charged for 
     *                              breaking the lease without transferring.  
     *                              Must be >= 0.
     */
    public StartLeaseEvent(Date leaseStart, String controllerName, 
            int amountPerMonth, String payerName, Date leaseEnd, 
            int paymentFrequency, int deposit, boolean canSublet, 
            String roomDescription, int leaseBreakPenalty) {
        super(leaseStart, controllerName, amountPerMonth);
        
        this.payerName = payerName;
        this.leaseEnd = leaseEnd;
        this.paymentFrequency = paymentFrequency;
        this.deposit = deposit;
        this.canSublet = canSublet;
        this.roomDescription = roomDescription;
        this.leaseBreakPenalty = leaseBreakPenalty;
    }
    

    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        StartLeaseEvent otherCasted = (StartLeaseEvent) other;
        value = payerName.compareTo(otherCasted.getPayerName());
        if (value != 0) {
            return value;
        }

        value = roomDescription.compareTo(otherCasted.getRoomDescription());
        if (value != 0) {
            return value;
        }
        
        value = boolToValue(canSublet) - boolToValue(otherCasted.isCanSublet());
        if (value != 0) {
            return value;
        }
        
        value = compareNumbers(otherCasted);
        
        return value;
    }


    /**
     * Compares the number values in this to those in the given StartLeaseEvent.
     * 
     * @param   other   the other StartLeaseEvent to compare to.  Cannot 
     *                  be null.
     *                      
     * @return  greater than 0 if this should come before other, less than 0 
     *          if this should come after other, and 0 if this is the same as 
     *          other.
     */
    private int compareNumbers(StartLeaseEvent other) {
        int value;
        value = compareLongs(leaseEnd.getTime(), other.getLeaseEnd().getTime());
        if (value != 0) {
            return value;
        }
        
        // original : 
        // value = paymentFrequency - other.getPaymentFrequency();
        // seed fault :
        value = MainTest.isOriginal || MainTest.isFaultFound ? paymentFrequency - other.getPaymentFrequency() :
        	paymentFrequency + other.getPaymentFrequency();
        
        if (value != 0) {
            return value;
        }
        
        value = deposit - other.getDeposit();
        if (value != 0) {
            return value;
        }
        
        value = leaseBreakPenalty - other.getLeaseBreakPenalty();
        if (value != 0) {
            return value;
        }
        
        return 0;
    }


    /**
     * {@inheritDoc}
     */
    public void process(FinanceManager manager) {
        manager.createLease(getDate(), getName(), payerName, getAmount(), 
                leaseEnd, paymentFrequency, deposit, canSublet, roomDescription,
                leaseBreakPenalty);
    }

    
    /**
     * Returns the name of the tenant.
     * 
     * @return  the name of the tenant.  Will not be null or empty.
     */
    public String getPayerName() {
        return payerName;
    }

    /**
     * Sets the name of the tenant.
     * 
     * @param   payerName   the name of the tenant.  Cannot be null or empty.
     */
    public void setPayerName(String payerName) {
        this.payerName = payerName;
    }

    
    /**
     * Returns the date the lease ends (exclusive).
     *  
     * @return  the date the lease ends.  Will not be null.
     */
    public Date getLeaseEnd() {
        return leaseEnd;
    }

    
    /**
     * Sets the date the lease ends (exclusive).
     * 
     * @param   leaseEnd    the date the lease ends.  Cannot be null.
     */
    public void setLeaseEnd(Date leaseEnd) {
        this.leaseEnd = leaseEnd;
    }

    
    /**
     * Returns the number of months between payments.
     * 
     * @return  the number of months between payments.  Will be >= 1.
     */
    public int getPaymentFrequency() {
        return paymentFrequency;
    }

    
    /**
     * Sets the number of months between payments.
     * 
     * @param   paymentFrequency    the number of months between payments.  
     *                              Must be >= 1.
     */
    public void setPaymentFrequency(int paymentFrequency) {
        this.paymentFrequency = paymentFrequency;
    }

    
    /**
     * Returns the amount of money for the deposit.
     * 
     * @return  the amount of money for the deposit.  Will be >= 0.
     */
    public int getDeposit() {
        return deposit;
    }

    
    /**
     * Sets the amount of money for the deposit.
     * 
     * @param   deposit the amount of money for the deposit.  Must be >= 0.
     */
    public void setDeposit(int deposit) {
        this.deposit = deposit;
    }

    
    /**
     * Returns whether the tenant can sublet.
     * 
     * @return  true if the tenant can sublet, false otherwise.
     */
    public boolean isCanSublet() {
        return canSublet;
    }

    
    /**
     * Sets whether the tenant can sublet.
     * 
     * @param   canSublet   true indicates that the tenant can sublet, false 
     *                      indicates that the tenant cannot sublet.
     */
    public void setCanSublet(boolean canSublet) {
        this.canSublet = canSublet;
    }

    
    /**
     * Returns the room description.
     * 
     * @return  the room description.  Will not be null.
     */
    public String getRoomDescription() {
        return roomDescription;
    }

    
    /**
     * Sets the room description.
     * 
     * @param   roomDescription the room description.  Cannot be null.
     */
    public void setRoomDescription(String roomDescription) {
        this.roomDescription = roomDescription;
    }


    /**
     * Returns the amount of money charged if the lease is broken.
     * 
     * @return  the amount of money to charge if the lease is broken.  Will be 
     *          >= 0.
     */
    public int getLeaseBreakPenalty() {
        return leaseBreakPenalty;
    }


    /**
     * Sets the amount of money to charge if the lease is broken.
     * 
     * @param   leaseBreakPenalty   the amount of money to charge if the lease 
     *                              is broken.  Must be >= 0.
     */
    public void setLeaseBreakPenalty(int leaseBreakPenalty) {
        this.leaseBreakPenalty = leaseBreakPenalty;
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
    
        super.validate(propertyName, manager);
        if (propertyName.equals("leaseEnd")) {
            instanceOfDate(propertyName, getLeaseEnd());
            if (!leaseEnd.after(getDate())) {
                throw new InvalidResultException(this, propertyName, 
                        "Must be after the lease start date.");
            }
        }
        else if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
        else if (propertyName.equals("payerName")) {
            resident(propertyName, getPayerName(), manager);
        }
        else if (propertyName.equals("deposit")) {
            notNullGreaterThanEqualInteger(propertyName, getDeposit(), 0);
        }
        else if (propertyName.equals("leaseBreakPenalty")) {
            notNullGreaterThanEqualInteger(propertyName, getLeaseBreakPenalty(), 0);
        }
        else if (propertyName.equals("paymentFrequency")) {
            notNullGreaterThanEqualInteger(propertyName, getPaymentFrequency(), 1);
        }
        else if (propertyName.equals("roomDescription")) {
            notNullOrEmptyString(propertyName, getRoomDescription());
        }
    }
}
